package com.sxtbbs.dao.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.sxtbbs.dao.DBDao;
import com.sxtbbs.util.JdbcUtils;

/**
 * 数据库数据的操作
 * @author lovemanai
 *
 */
public class DBDaoImpl implements DBDao {
	private static Connection conn;

	static {
		try {
			conn = JdbcUtils.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 执行一条增删改语句
	 * @param sql 执行的sql语句，形如:insert into table value(?,?,?)语句中含有?占位符
	 * @param obj 对象数组，用来替换sql语句中的?占位符
	 */
	public int excuteDML(String sql,  Object... obj) {
		
		PreparedStatement pstmt = null;
		int result = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			for(int i = 1; i <= obj.length; i++) {
				pstmt.setObject(i, obj[i - 1]);
			}
			result =  pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 执行一条查询语句
	 * @param sql 查询语句
	 * @return 返回查询获得结果集
	 */
	public ResultSet excuteSelect(String sql, Object... params) {

		ResultSet rs = null;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for(int i = 1; i < params.length; i++) {
				pstmt.setObject(i - 1, params[i]);
			}
			
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public List<?> query(String sql, Class<?> clazz, Object... params) {
		List<Object> list = new ArrayList<Object>();
		ResultSet rs = null;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for (int i = 1; i <= params.length; i++) {
				pstmt.setObject(i, params[i]);
			}
			rs = pstmt.executeQuery();
			Field[] fields = clazz.getDeclaredFields();
			Constructor<?> cons = clazz.getConstructor();
			while(rs.next()) {
				Object obj = cons.newInstance();
				for (Field field : fields) {
					String fieldName = field.getName();
					Method method = clazz.getMethod("set" + fieldName.substring(0,1).toUpperCase()+ fieldName.substring(1),
							field.getType());
					method.invoke(obj, rs.getObject(fieldName));
				}
				list.add(obj);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			close(conn);
		}
		return list;
		
	}
	
	/**
	 * 关闭Connection, Statement, ResultSet
	 * @param obj 连接对象
	 */
	public void close(Object... obj) {
		for(int i = 0; i < obj.length; i++) {
			String type = obj[i].getClass().getTypeName();
			try {
			if(type.equals("com.mchange.v2.c3p0.impl.NewProxyConnection")) {
					Connection co = (Connection)obj[i];
					if(co != null) co.close();
				} else if(type.equals("java.sql.ResultSet")) {
					ResultSet rs = (ResultSet)obj[i];
					if(rs != null) rs.close();
				} else if(type.equals("java.sql.Statement")) {
					Statement stat = (Statement)obj[i];
					if(stat != null) stat.close();
				} else if(type.equals("java.sql.PreparedStatement")) {
					PreparedStatement pstmt = (PreparedStatement)obj[i];
					if(pstmt != null) pstmt.close();
				}
			}catch(SQLException e) {
				
			}
		}
	}
 }
